'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 公会信息列表
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var unionGroupModel = require('../../../../models/unionGroupModel');
var PlayerManager = require('../../../../libs/playerManager');
var unionGroupConf = require('../../../../config/unionGroupConf');
var unionGroupService = require('../../../../services/unionGroupService');
var onlineService = require('../../../../services/onlineService.js');
var roleModel = require('../../../../models/roleModel.js');
/**
 * 公会信息列表
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var union_msg;
    var role_model;
    var union_id;
    var my_position = 3;
    var apply_state = 0;
    var union_group_model;
    var create_role_id = 0;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                role_model = player_manager.role;
                union_id = role_model.getUnionId();

                if (!union_id) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                union_group_model = model;
                create_role_id = union_group_model.create_role_id || 0;
                cb();
            },
            function (cb) {
                roleModel.getByRoleId(create_role_id, cb);
            },
            function (boss_role_model, cb) {
                _.some(union_group_model.union_user_list, function (item) {
                    if (item.role_id == role_id) {
                        my_position = item.position;
                    }
                });

                var member_limit_num = unionGroupConf.getUnionMemberLimitNum(union_group_model.getUnionLv());
                union_msg = {
                    union_announce: union_group_model.union_announce || '',
                    lv: union_group_model.lv,
                    union_id: union_group_model.union_id,
                    unix_contribution: union_group_model.unix_contribution,
                    union_exp: union_group_model.union_exp,
                    union_max_exp: unionGroupConf.getUnionLvExp(union_group_model.getUnionLv()),
                    union_flag: union_group_model.union_flag,
                    create_role_id: union_group_model.create_role_id,
                    kick_times: union_group_model.kick_times,
                    limit_level: union_group_model.limit_level,
                    union_create_time: union_group_model.union_create_time,
                    name: union_group_model.name,
                    my_position: my_position,
                    boss_name: boss_role_model.name,
                    union_max_user_num: member_limit_num,
                    now_union_user_num: union_group_model.union_user_list.length
                };

                if (!!union_group_model.isBoss(role_id) && !!union_group_model.apply_list.length) {
                    apply_state = 1;
                }
                cb();
            }

        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    union_msg: union_msg,
                    apply_state: apply_state
                }
            });
        }
    );
};
